﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel.Design;
using System.Resources;

namespace Common
{
    public static class Functions
    {
        public static string GlossaryKey(string key, LANGUAGE language = LANGUAGE.VIETNAMESE)
        {
            var resourceSet = Resource.GlossaryFL.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, false, false);

            if (language == LANGUAGE.ENGLISH)
                return Resource.GlossaryFL.ResourceManager.GetString(key);
            else
                return Resource.Glossary.ResourceManager.GetString(key);
        }

        /// <summary>
        /// mapping the value from original to target entity trustly.
        /// only comparing the name is matched and the property has Get&Set method
        /// </summary>
        /// <typeparam name="T">target entity type</typeparam>
        /// <typeparam name="O">original entity type</typeparam>
        /// <param name="originalEntity">the entity will be copied the values</param>
        /// <param name="targetEntity">the entity will be received the values</param>
        /// <returns>the target entity</returns>
        public static T TrustlyMapping<T, O>(O originalEntity, T targetEntity)
        {
            var propOrgList = typeof(O).GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
            var propOrgNameList = propOrgList.Select(p => p.Name).ToList();
            var propTagList = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();

            var intersectProperties = propTagList.Where(p => propOrgNameList.Contains(p.Name)).ToList();

            PropertyInfo orgProperty = null;
            intersectProperties.ForEach(p =>
                {
                    orgProperty = propOrgList.FirstOrDefault(t => t.Name == p.Name);

                    if (p.GetSetMethod(false) != null && orgProperty.GetSetMethod(false) != null)
                    {
                        p.SetValue(targetEntity, orgProperty.GetValue(originalEntity));
                    }
                });

            return targetEntity;
        }
    }
}
